﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using Cashier_DAO.db;
using Cashier_Model;
using Cashier_DAO;

namespace Cashier_Service
{
    public class GuidService
    {
        private GuidDAO guidDAO;

        private GuidService()
        {
            guidDAO = new GuidDAO();
        }

        /// <summary>
        /// 新增GUID
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private long InsertGuid(String name)
        {
            long id = 0;
            try
            {
                lock(guidDAO)
                {
                    using (DbConnection conn = ConnectionFactory.GetDbConnection())
                    {
                        conn.Open();
                        SysGuid sysGuid = new SysGuid();
                        sysGuid.Name = name;
                        sysGuid.CreateTime = DateTime.Now;
                        id = guidDAO.InsertGuid(conn, sysGuid);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 获取GUID
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static long GetGUID(String name)
        {
            return new GuidService().InsertGuid(name);
        }
    }
}
